<div class="system-messages">
    <ul>
        <?php
        $messages = $this->messages;
        foreach ($messages as $message) :
            ?>
            <li><?= $message ?></li>
        <?php
        endforeach
        ?>
    </ul>
</div>
<div>
    <div class="admin-search-n-filter">
        <div class="admin-search">
            <label><input type="text" value="<?= $this->enteredKeyWords ?>"/></label>
            <input type="button" value=""/>
        </div>
        <div class="admin-hyperlink-btn">
            <a href="/classico/public/admin/product/index">
                Refresh
            </a>
        </div>
        <div class="admin-filter">
            <label>
                <select>
                    <option value="0">-- Product Type --</option>
                    <?php
                    /** @var ProductTypePOJO[] $productTypePOJOs */
                    $productTypePOJOs = $this->productTypePOJOs;
                    foreach ($productTypePOJOs as $productTypePOJO) :
                        if ($this->selectedProductTypeID &&
                            $productTypePOJO->ID == $this->selectedProductTypeID
                        ) :
                            ?>
                            <option value="<?= $productTypePOJO->ID ?>"
                                    selected="selected">
                                <?= $productTypePOJO->Name ?>
                            </option>
                        <?php
                        endif;
                        if (!$this->selectedProductTypeID ||
                            $productTypePOJO->ID != $this->selectedProductTypeID
                        ) :
                            ?>
                            <option value="<?= $productTypePOJO->ID ?>">
                                <?= $productTypePOJO->Name ?>
                            </option>
                        <?php
                        endif;
                    endforeach
                    ?>
                </select>
            </label>
        </div>
    </div>
    <div class="datagrid">
        <table>
            <tr>
                <th width="2%"><label><input id="check-box-all" type="checkbox"/></label></th>
                <th width="2%">ID</th>
                <th width="31%">Name</th>
                <th width="11%">Created Date</th>
                <th width="11%">Expired</th>
                <th width="11%">Orginal Amount</th>
                <th width="11%">Existing Amount</th>
                <th>Old Price</th>
                <th>New Price</th>
                <th width="5%" class="center">Delete</th>
            </tr>
            <?php
            $products = $this->products;
            foreach ($products as $index => $product) :
                ?>
                <tr class="<?= ($index % 2 == 0) ? 'alt' : '' ?>">
                    <td>
                        <label>
                            <input class="check-box-product-id" type="checkbox"
                                   value="<?= $product['ID'] ?>"/>
                        </label>
                    </td>
                    <td><?= $product['ID'] ?></td>
                    <td>
                        <a href="/classico/public/admin/product/update/productID/<?= $product['ID'] ?>">
                            <?= $product['Name'] ?>
                        </a>
                    </td>
                    <td><?= $product['CreatedDate'] ?></td>
                    <td><?= $product['Expired'] ?></td>
                    <td><?= $product['OrginalAmount'] ?></td>
                    <td><?= $product['ExistingAmount'] ?></td>
                    <td><?= $product['OldPrice'] ?></td>
                    <td><?= $product['NewPrice'] ?></td>
                    <td class="center">
                        <div class="enabled-delete-icon tool-icon"></div>
                        <form action="/classico/public/admin/product/delete" method="post">
                            <input type="hidden" name="isPost" value=""/>
                            <input type="hidden" name="productIDs[]" value="<?= $product['ID'] ?>"/>
                        </form>
                    </td>
                </tr>
            <?php
            endforeach
            ?>
        </table>
    </div>
</div>
<?= $this->paginationControl($this->paginator, 'Sliding', 'my_pagination_control.phtml') ?>
<script type="text/javascript">
    $('#tool-btn-confirm, #tool-btn-unblock, #tool-btn-block')
        .removeClass('enabled-tool-btn');
    $('#tool-btn-confirm > div')
        .removeClass('enabled-confirm-icon')
        .addClass('disabled-confirm-icon');
    $('#tool-btn-unblock > div')
        .removeClass('enabled-unblock-icon')
        .addClass('disabled-unblock-icon');
    $('#tool-btn-block > div')
        .removeClass('enabled-block-icon')
        .addClass('disabled-block-icon');
</script>
<script type="text/javascript">
    function forwardHyperlink() {
        var currentURL = window.location.href;

        var forwardURL = currentURL.replace(/\/product\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/index\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/keyWords\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/productTypeID\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/page\/[\w\W]*/, '');

        forwardURL += '/product/index';

        var keyWords = $('.admin-search input[type = "text"]').val();
        if (keyWords != '') {
            forwardURL += '/keyWords/' + keyWords;
        }

        var productTypeID = $('.admin-filter select').val();
        if (productTypeID != 0) {
            forwardURL += '/productTypeID/' + productTypeID;
        }

        window.location.href = forwardURL;
    }

    $('.admin-search input[type = "text"]').keydown(function (event) {
        if (event.which == 13) {
            forwardHyperlink();
        }
    });

    $('.admin-search input[type = "button"]').click(function () {
        forwardHyperlink();
    });

    $('.admin-filter select').change(function () {
        forwardHyperlink();
    });
</script>
<script type="text/javascript">
    $('#check-box-all').click(function () {
        if ($(this).attr('checked')) {
            $('.check-box-product-id').attr('checked', true);
        } else {
            $('.check-box-product-id').attr('checked', false);
        }
    });

    $('.datagrid .tool-icon').click(function () {
        $(this).parent(null).find('form').submit();
    });
</script>
<script type="text/javascript">
    $('#tool-btn-delete.enabled-tool-btn').click(function () {
        var form = $(this).find('form');

        var checkedCheckBoxes = $('.check-box-product-id:checked');
        for (var i = 0; i < checkedCheckBoxes.length; i++) {
            var productID = checkedCheckBoxes[i].value;
            var tag = '<input type="hidden" name="productIDs[]" value="' + productID + '"/>';
            form.append(tag);
        }

        form.submit();
    });
</script>